#!/usr/bin/env bash
#$ -N heudiconv
#$ -o { project_root }/logs
#$ -q { queue }
#$ -j y
#$ -t 1:{ num_jobs }
#$ -tc { max_jobs }

export PROJECT_ROOT={ project_root }
read SUBJECT SESSION <<< $(awk -v id=$SGE_TASK_ID 'NR==id {{print $1, $2}}' { file_sublist })
export SUBJECT SESSION

# clean cache if exists
SUBLABEL=`{ path_template }/heudiconv/format_subject.py ${{SUBJECT}}`
cache_dir={ path_raw }/.heudiconv/${{SUBLABEL}}/ses-${{SESSION}}
if [ -d ${{cache_dir}} ]; then
    echo [`date "+%Y-%m-%d %H:%M:%S"`] "Remove heudiconv caches in ${{dir}} from previous run for subject: ${{SUBJECT}}."
    rm -rf ${{cache_dir}}
fi
echo [`date "+%Y-%m-%d %H:%M:%S"`] "Begin heudiconv for subject: ${{SUBJECT}}, session: ${{SESSION}}."
{ path_template }/exec_heudiconv.sh
echo [`date "+%Y-%m-%d %H:%M:%S"`] "End heudiconv for subject: ${{SUBJECT}}, session: ${{SESSION}}."
